<?php
/**
 * Created by PhpStorm.
 * User: tao
 * Date: 2016/11/30
 * Time: 下午9:31
 */
namespace Api\Controller;


require_once 'vendor/autoload.php';

use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;  

class StudentController extends BaseController{
    public function _initialize() {
        parent::_initialize();
        $a            = (String) $_SERVER['HTTP_TOKEN'];
        $token        = empty($a) ? I('token') : $a;
        if($token) {
          $parse        = (new Parser())->parse($token);
          $signer       = new Sha256();
          $this->parse  = $parse;
          $this->uid    = $this->parse->getClaim('uid');
        }
    }

    public function index() {
    	switch ($_SERVER['REQUEST_METHOD']) {
    		case 'GET':
          $id    = I('id');
    			if($id) {
            $this->get_stu($id);
          } else {
            
          }
    			break;
    		case 'POST':
          $this->post_stu();
    			break;
    		default:
    			break;
    	}
    }

    public function get_stu($id) {
      $map['idCard']        = $id;
      $rs                   = M('student')->where($map)->find();
      $array                = array(
              'status'      => 0,
              'msg'         => '获取成功',
              'data'        => 'NULL'
      );

      

      if(!$rs) {
        $array['status']    = 404;
        $array['msg']       = '获取失败';
      }

      $array['data']        = $rs;

      $this->ajaxReturn($array);
    }

    public function post_stu() {
      $data                 = $_POST;
      $array                = array(
          'status'          => 0,
          'msg'             => '更新成功'
      );


      $map['idCard']        = $data['idcard'];

      if($data['idcard'] != $this->uid){
          $array['status']  = 404;
          $array['msg']     = '别随意修改身份证';
          $this->ajaxReturn($array);
      }


      // 数据库命名问题导致这个操作 卧槽
      $data1['className']      = $data['classname'];
      $data1['speName']        = $data['spename'];
      $data1['dormitoryGrade'] = $data['dormitorygrade'];
      $data1['studentNumber']  = $data['studentnumber'];
      $data1['studentName']    = $data['studentname'];
      $data1['studentSex']     = $data['studentsex'];
      $data1['report']         = $data['report'];
      $data1['sick']           = $data['sick'] * 1;
      $data1['prove']          = $data['prove'];
      $data1['insurance']      = $data['insurance'] * 1;
      $data1['forecast']       = 1;

      $f                    = M('student')->where($map)->save($data1);

      if(!$f) {
          $array['status']  = 404;
          $array['msg']     = '更新失败';
      }

      $array['data']        = $f;

      $this->ajaxReturn($array);
    }

    public function delete_stu() {

    }

   public function login() {
       $u          = I('username');
       $p          = I('password');
       $data       = [
           'status' => '',
           'data'   => ''
       ];
       if(empty($u)) {
           $data['status'] = 1;
           $data['msg']    = '姓名不能为空';
           $data['data']   = 'null';
        }

        if(empty($p)) {
           $data['status'] = 2;
           $data['msg']    = '身份证不能为空';
           $data['data']   = 'null';
        }
        $user = M('student');

        if(($user->where(array('mobile' => $u))->count()) >= 1) {
           $rs = $user->where(array('studentName' => $u, 'idCard' => $p))->find();
           if(!$rs) {
               $data['status'] = 3;
               $data['msg']    = '身份证 或 姓名有误';
               $data['data']   = 'null';
           } else {

              $builder = new Builder();
              $signer = new Sha256();
              // 设置发行人
              $builder->setIssuer('http://example.com'); 
              // 设置接收人
              $builder->setAudience('http://example.org'); 
              // 设置id
              $builder->setId('4f1g23a12aa', true); 
              // 设置生成token的时间
              $builder->setIssuedAt(time()); 
              // 设置在60秒内该token无法使用
              $builder->setNotBefore(time() + 60); 
              // 设置过期时间
              $builder->setExpiration(time() + 3600); 
              // 给token设置一个id

              $builder->set('uid', $rs['idcard']); 
              // 对上面的信息使用sha256算法签名
              $builder->sign($signer, 'can_contact');
              // 获取生成的token
              $token = $builder->getToken();

              session('uid', $rs);
              cookie('token', $token);              

              $map['token']                    = (String) $token;
              $rs['classname']                 = array($rs['classname']);
              $map['userInfo']                 = $rs;

              $data['status']                           = 0;
              $data['msg']                              = '登入成功';
              $data['data']                             = $map;
           }
        } else {
            $data['status']                           = 4;
            $data['msg']                              = '没有这个学生哦';
            $data['data']                             = 'null';
        }



       $this->ajaxReturn($data);
    }

     public function sign_in() {
      $rs = M('student')->where('idCard='.$this->uid)->find();

      $array                    = array(
        'status'              => 1,
        'msg'                 => '已经确认信息!赶紧去查看分配的的宿舍吧'
      );

      $rs['classname']          = array($rs['classname']);

      if($rs && $rs['forecast'] == '0') {
        $array['msg']           = '还未确认信息!赶紧去确认信息吧';
        $array['status']        = 2;
      }

      $array['data']            = $rs;

      $this->ajaxReturn($array);
    }
}